#include <bits/stdc++.h>

//#pragma GCC optimize(2)
#define For(x, y, z) for (int x = y; x <= z; ++x)
#define Rof(x, y, z) for (int x = y; x >= z; --x)
#define Eor(u) for (int i = head[u]; i; i = nxt[i])
#define SZ(x) (int(x.size()))
#define pb push_back

using namespace std;
using i64 = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)

inline int read() {
	int x = 0, f = 0; char ch = getchar();
	while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
	while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
	return f ? -x : x;
}
int __stk[128], __tp;
inline void put(i64 x) {
	if (x < 0) putchar('-'), x = -x;
	do { __stk[++__tp] = x % 10, x /= 10; } while (x);
	while (__tp) putchar(__stk[__tp--] + '0');
}

const int mod = 998244353;
inline int ksm(int x, int y, int res = 1) {
	for ( ; y; y >>= 1, x = 1ll * x * x % mod)
		(y & 1) && (res = 1ll * res * x % mod);
	return res;
}
inline int inv(int x) { return ksm(x, mod - 2); }
inline int gcd(int a, int b) { if (b) while ((a %= b) && (b %= a)); return a | b; }
inline void add(int &x, int y) { (x += y) >= mod && (x -= mod); }
inline void Min(int &x, int y) { (y < x) && (x = y); }
inline void Max(int &x, int y) { (y > x) && (x = y); }

const int N = 410;
int n, p[N];
i64 a[N], s[N];
vector<int> ve[N];

namespace part1 {
    i64 ans;
    void solve() {
        For(i, 1, n) p[i] = i;
        do { i64 res = 0;
            For(i, 1, n) s[i] = a[i];
            For(i, 1, n) { 
                res += s[p[i]];
                for (auto v : ve[p[i]]) 
                    s[v] += s[p[i]];
            } ans = max(ans, res);
        } while (next_permutation(p + 1, p + n + 1));
        cout << ans;
    }
}


i64 ans, res;
void dfs(int u, int fa) {
    for (int v : ve[u]) if (v != fa) 
        dfs(v, u), s[u] += s[v];
    res += s[u];
}
signed main() {
	freopen("light.in", "r", stdin);
	freopen("light.out", "w", stdout);

    n = read(); For(i, 1, n) a[i] = read();
    For(i, 2, n) {
        int u = read(), v = read();
        ve[u].pb(v), ve[v].pb(u);
    }
    if (n <= 10) part1::solve(), exit(0);
    
    For(i, 1, n) {
        For(j, 1, n) s[j] = a[j];
        dfs(i, 0), ans = max(ans, res);
    }
    cout << ans;
	return 0;
}